package testPackage.Driver;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GraduatedDriver extends FirefoxDriver {
	
	public int Timeout = 45;
	  
	public void Click(WebElement element)
	{
			
		WaitForElementPresent(element);
       
        element.click();
	}
	
	public void Click(By locator)
	{
		WaitForElementPresent(locator);
		findElement(locator).click();
	}
	
	 public void Input(WebElement element, String sendText)
     {
         if (sendText == null)
         {
             return;
         }
         element.clear();
         element.sendKeys(sendText);
     }
	 
	 public void WaitForAjax()
     {
         int counter = 0;
         while (counter < Timeout)
         {
             try
             {
            	 JavascriptExecutor js = (JavascriptExecutor) this;
            	 boolean ajaxIsComplete = (Boolean)js.executeScript("return jQuery.active == 0");
                 if (ajaxIsComplete)
                 {
                     break;
                 }
                 Thread.sleep(1000);
                 counter++;
             }
             catch (Exception e)
             {
             }
         }
     }
	 
	 public void WaitForElementPresent(By locator)
	 {
		 WebDriverWait wait = new WebDriverWait(this, Timeout);
         wait.until(ExpectedConditions.presenceOfElementLocated(locator));
	 }
	 
	 public void WaitForElementPresent(WebElement element)
	 {
		 WebDriverWait wait = new WebDriverWait(this, Timeout);
         wait.until(ExpectedConditions.visibilityOf(element));
    
	 }
}
